// pages/detailList/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    actionID: '',
    signArr: [],
    seletedNum: '',
    isMorethan: true,
    loadding: true,
    showNum: false,
    url: ''
  },

  getSignList: function() {
    let that = this
    wx.cloud.callFunction({
      name: 'getSignList',
      data: {
        actionID: that.data.actionID
      },
      success: res => {
        let arr = res.result.data
        if(res.result.data.length === 0) {
          wx.hideLoading({
            success: (res) => {},
          })
          return
        }
        if(arr.length > 15) {
          that.setData({
            isMorethan: false
          })
        }
        wx.hideLoading({
          success: (res) => {
            wx.showToast({
              title: '加载成功',
            })
            that.setData({
              signArr: arr,
              loadding: false
            })},
        })
      }
    })
  },
  luck() {
    let length = this.data.signArr.length
    const that = this
    let arr = []
    for(let i = 0; i < 20; i++) {
      let num = Math.ceil(Math.random()*(length))
      arr.push({
        num: num,
        url: that.data.signArr.slice(arr[i]-1, arr[i])[num - 1].avatarUrl
      })
    }
    let i = 0 
    this.setData({
      showNum: true
    })
    let set = setInterval(() => {
      let a = i++
      let data = arr[a]
      that.setData({
        seletedNum: data.num,
        url: data.url
      })
      if( i == 20) {
        clearInterval(set) 
      }
    }, 200);
  },
  gotoShowMore() {
    let that = this
    wx.navigateTo({
      url: '../showAll/index?actionID=' + that.data.actionID
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    let that = this
    if(options.actionID) {
      this.setData({
        actionID: options.actionID
      }, () => {
        that.getSignList()
      })
    }else {
      wx.showToast({
        title: '加载失败，请联系管理员',
        icon: 'none',
        duration: 3000
      })
      return 
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})